package com.fangzhi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fangzhi.mapper.AdminAccountRoleDeptRelationMapper;
import com.fangzhi.mobel.AdminAccountRoleDeptRelation;
import com.fangzhi.mobel.DeptBaseInfo;
import com.fangzhi.mobel.DeptBaseInfo;
import com.fangzhi.mobel.vo.LoginVo;
import com.fangzhi.mobel.vo.DeptListVo;
import com.fangzhi.service.DeptBaseInfoService;
import com.fangzhi.mapper.DeptBaseInfoMapper;
import com.fangzhi.utils.BeanCopyUtil;
import com.fangzhi.utils.GlobalParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author edy
* @description 针对表【dept_base_info(部门表)】的数据库操作Service实现
* @createDate 2024-09-11 20:07:35
*/
@Service
public class DeptBaseInfoServiceImpl extends ServiceImpl<DeptBaseInfoMapper, DeptBaseInfo>
    implements DeptBaseInfoService{

    @Autowired
    private AdminAccountRoleDeptRelationMapper adminAccountRoleDeptRelationMapper;

    @Override
    public List<DeptListVo> deptList(Integer deptId) {
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        AdminAccountRoleDeptRelation adminAccountRoleDeptRelation = adminAccountRoleDeptRelationMapper.selectOne(Wrappers
                .<AdminAccountRoleDeptRelation>lambdaQuery().eq(AdminAccountRoleDeptRelation::getAdminAccounntId, loginVo.getId()));
        List<DeptBaseInfo> DeptBaseInfos;
        if (adminAccountRoleDeptRelation.getRoleId() == 1 || adminAccountRoleDeptRelation.getRoleId() == 6){
           DeptBaseInfos = this.getBaseMapper().selectList(Wrappers.<DeptBaseInfo>lambdaQuery()
                    .eq(deptId > 0, DeptBaseInfo::getId, deptId));
        }else if (adminAccountRoleDeptRelation.getRoleId() == 4 || adminAccountRoleDeptRelation.getRoleId() == 5){
            DeptBaseInfos = this.getBaseMapper().selectList(Wrappers.<DeptBaseInfo>lambdaQuery()
                    .eq(deptId > 0, DeptBaseInfo::getId, deptId));
        }else{
            DeptBaseInfos = this.getBaseMapper().selectList(Wrappers.<DeptBaseInfo>lambdaQuery()
                    .eq(deptId > 0, DeptBaseInfo::getId, deptId).eq(DeptBaseInfo::getId, adminAccountRoleDeptRelation.getDeptId()));
        }
        List<DeptListVo> collect = DeptBaseInfos.stream().map(T -> new DeptListVo()
                .setId(T.getId()).setLabel(T.getDeptName())).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<DeptListVo> deptList(String deptId) {
        List<Integer> list = JSON.parseArray(deptId, Integer.class);
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        AdminAccountRoleDeptRelation adminAccountRoleDeptRelation = adminAccountRoleDeptRelationMapper.selectOne(Wrappers
                .<AdminAccountRoleDeptRelation>lambdaQuery().eq(AdminAccountRoleDeptRelation::getAdminAccounntId, loginVo.getId()));
        List<DeptBaseInfo> DeptBaseInfos;
        if (adminAccountRoleDeptRelation.getRoleId() == 1 || adminAccountRoleDeptRelation.getRoleId() == 6){
            DeptBaseInfos = this.getBaseMapper().selectList(Wrappers.<DeptBaseInfo>lambdaQuery()
                    .in(!list.isEmpty(), DeptBaseInfo::getId, list));
        }else if (adminAccountRoleDeptRelation.getRoleId() == 4 || adminAccountRoleDeptRelation.getRoleId() == 5){
            DeptBaseInfos = this.getBaseMapper().selectList(Wrappers.<DeptBaseInfo>lambdaQuery()
                    .in(!list.isEmpty(), DeptBaseInfo::getId, list));
        }else{
            DeptBaseInfos = this.getBaseMapper().selectList(Wrappers.<DeptBaseInfo>lambdaQuery()
                    .in(!list.isEmpty(), DeptBaseInfo::getId, list).eq(DeptBaseInfo::getId, adminAccountRoleDeptRelation.getDeptId()));
        }
        return DeptBaseInfos.stream().map(T -> new DeptListVo()
                .setId(T.getId()).setLabel(T.getDeptName())).collect(Collectors.toList());
    }

    @Override
    public List<DeptListVo> deptList1(String deptId) {
        List<String> list =Arrays.asList(deptId.split(","));
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        AdminAccountRoleDeptRelation adminAccountRoleDeptRelation = adminAccountRoleDeptRelationMapper.selectOne(Wrappers
                .<AdminAccountRoleDeptRelation>lambdaQuery().eq(AdminAccountRoleDeptRelation::getAdminAccounntId, loginVo.getId()));
        List<DeptBaseInfo> DeptBaseInfos;
        if (adminAccountRoleDeptRelation.getRoleId() == 1 || adminAccountRoleDeptRelation.getRoleId() == 6){
            DeptBaseInfos = this.getBaseMapper().selectList(Wrappers.<DeptBaseInfo>lambdaQuery()
                    .in(!list.isEmpty(), DeptBaseInfo::getId, list));
        }else if (adminAccountRoleDeptRelation.getRoleId() == 4 || adminAccountRoleDeptRelation.getRoleId() == 5){
            DeptBaseInfos = this.getBaseMapper().selectList(Wrappers.<DeptBaseInfo>lambdaQuery()
                    .in(!list.isEmpty(), DeptBaseInfo::getId, list));
        }else{
            DeptBaseInfos = this.getBaseMapper().selectList(Wrappers.<DeptBaseInfo>lambdaQuery()
                    .in(!list.isEmpty(), DeptBaseInfo::getId, list).eq(DeptBaseInfo::getId, adminAccountRoleDeptRelation.getDeptId()));
        }
        List<DeptListVo> collect = DeptBaseInfos.stream().map(T -> new DeptListVo()
                .setId(T.getId()).setLabel(T.getDeptName())).collect(Collectors.toList());
        return collect;
    }
}




